﻿
using Authorization.Center.Controllers;
using Authorization.Center.SqlServer;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S.PurchaseAndSales.Api.Application.Command.PurchaseFormCommand;
using S.PurchaseAndSales.Api.Application.Command.SheepSaleCommand;
using S.PurchaseAndSales.Api.Application.CommandHandlers.SheepSaleHandler;
using S.PurchaseAndSales.Domain.Model;

namespace S.PurchaseAndSales.Api.Controllers
{
    /// <summary>
    /// 采购与销售--写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PurchaseAndSalesController : BaseController
    {
        private readonly IMediator mediator;
        public PurchaseAndSalesController(MySqlDbContext _db, IMediator mediator) : base(_db)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 测试Apollo是否可以使用
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult ApolloTest([FromServices] IConfiguration configuration, string key)
        {
            return Ok(configuration.GetValue<string>(key));
        }

        /// <summary>
        /// 羊只采购添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<bool> AddPurchaseForm(PurchaseFormAddCommand command)
        {
            foreach(var item in command.PurchaseFormList)
            {
                item.CreateBy = UserStrategyDto.UserTrueName;
                item.CreateTime = DateTime.Now;
            }
            return await mediator.Send(command);
        }

        /// <summary>
        /// 羊只销售添加
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> AddSheppSale(AddSheepSaleCommand comm)
        {
            comm.CreateBy = UserStrategyDto.UserTrueName;
            comm.CreateDate = DateTime.Now;
            return Ok(await mediator.Send(comm));
        }

        /// <summary>
        /// 客户信息添加
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> AddCustomer(AddCustomerCommand comm)
        {
            comm.CreateBy = UserStrategyDto.UserTrueName;
            comm.CreateDate = DateTime.Now;
            return Ok(await mediator.Send(comm));
        }

        /// <summary>
        /// 客户删除
        /// </summary>
        /// <param name="vendorSnowId"></param>
        /// <returns></returns>
        [Authorize]
        [HttpDelete]
        public async Task<IActionResult> DeleteCustom(int customSnowId)
        {
            return Ok(await mediator.Send(new CustomDeleteCommand() { CustomId = customSnowId }));
        }

        /// <summary>
        /// 羊只销售批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Authorize]
        [HttpDelete]
        public async Task<IActionResult> DeleteShepp(string ids)
        {
            return Ok(await mediator.Send(new DeleteSheepSaleCommand() { ids = ids }));
        }

        /// <summary>
        /// 供应商添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<bool> AddVendorAsync(VendorAddCommand command)
        {
            command.CreateBy = UserStrategyDto.UserTrueName;
            return await mediator.Send(command);
        }

        /// <summary>
        /// 供应商删除
        /// </summary>
        /// <param name="vendorSnowId"></param>
        /// <returns></returns>
        [Authorize]
        [HttpDelete]
        public async Task<IActionResult> DeleteVendor(int vendorSnowId)
        {
            return Ok(await mediator.Send(new VendorDeleteCommand() { VendorSnowId=vendorSnowId }));
        }
        /// <summary>
        /// 修改羊只销售(仓库审核)
        /// </summary>
        /// <param name="saleSheepUpdateCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<IActionResult> UpdateSheepSale(SaleSheepUpdateCommand saleSheepUpdateCommand)
        {
            saleSheepUpdateCommand.UpdateBy= UserStrategyDto.UserTrueName;
            saleSheepUpdateCommand.UpdateDate = DateTime.Now;
            saleSheepUpdateCommand.WorkShopCheckDate = DateTime.Now;
            saleSheepUpdateCommand.WorkShopCheckPeople = UserStrategyDto.UserTrueName;
            return Ok(await mediator.Send(saleSheepUpdateCommand));
        }
        /// <summary>
        /// 修改羊只销售（编辑）
        /// </summary>
        /// <param name="saleSheepUpdateCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<IActionResult> UpdateSaleSheep(SaleSheepUpdateCommand saleSheepUpdateCommand)
        {
            saleSheepUpdateCommand.UpdateBy = UserStrategyDto.UserTrueName;
            saleSheepUpdateCommand.UpdateDate = DateTime.Now;
            return Ok(await mediator.Send(saleSheepUpdateCommand));
        }
        /// <summary>
        /// 修改状态（部门审批、审批状态）
        /// </summary>
        /// <param name="saleSheepUpdateCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<IActionResult> UpdateCheckState(SaleSheepUpdateCheckStateCommand saleSheepUpdateCommand)
        {
            saleSheepUpdateCommand.UpdateDate= DateTime.Now;
            saleSheepUpdateCommand.UpdateBy = UserStrategyDto.UserTrueName;
            saleSheepUpdateCommand.CheckDate = DateTime.Now;
            saleSheepUpdateCommand.CheckPeople = UserStrategyDto.UserTrueName;
            return Ok(await mediator.Send(saleSheepUpdateCommand));
        }
        /// <summary>
        /// 修改状态（财务审批、审批状态财务）
        /// </summary>
        /// <param name="saleSheepUpdateCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<IActionResult> UpdateMoneyCheckState(SaleSheepUpdateMoneyCommand  saleSheepUpdateMoneyCommand)
        {
            saleSheepUpdateMoneyCommand.UpdateDate = DateTime.Now;
            saleSheepUpdateMoneyCommand.UpdateBy = UserStrategyDto.UserTrueName;
            saleSheepUpdateMoneyCommand.MoneyCheckDate = DateTime.Now;
            saleSheepUpdateMoneyCommand.MoneyCheckPeople= UserStrategyDto.UserTrueName;
            return Ok(await mediator.Send(saleSheepUpdateMoneyCommand));
        }

        /// <summary>
        /// 采购表修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<bool> UpdatePurchaseForm(PurchaseFormUpdateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 采购表删除
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        public async Task<bool> DeletePurchaseForm(string purchaseFormId, string updateBy)
        {
            return await mediator.Send(new PurchaseFormDeleteCommand()
            {
                PurchaseFormIds = purchaseFormId,
                UpdateBy = updateBy
            });
        }

        /// <summary>
        /// 财务审核
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="updateBy"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<bool> AuditPurchaseFormByIds(string ids, string updateBy)
        {
            return await mediator.Send(new PurchaseFormAuditCommand()
            {
                PurchaseFormIds = ids,
                UpdateBy = updateBy
            });
        }

        /// <summary>
        /// 仓库审核
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<bool> RoundUpPurchaseForm(string ids, string updateBy)
        {
            return await mediator.Send(new PurchaseFormRoundUpCommand()
            {
                PurchaseFormIds = ids,
                UpdateBy = updateBy
            });
        }
        /// <summary>
        /// 客户的批量添加
        /// </summary>
        /// <param name="customBAdd"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<bool> BatchAddCustom(CustomBAddCommand customBAdd)
        {
            return await mediator.Send(customBAdd);
        }
        /// <summary>
        /// 批量修改销售状态
        /// </summary>
        /// <param name="sheepSaleBUpdateMoneyStateCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<bool> BatchUpdateMoneySheepSale(SheepSaleBUpdateMoneyStateCommand sheepSaleBUpdateMoneyStateCommand)
        {
            foreach(var item in sheepSaleBUpdateMoneyStateCommand.sheepSaleModels)
            {
                item.UpdateBy = UserStrategyDto.UserTrueName;
                item.UpdateDate = DateTime.Now;
                item.MoneyCheckPeople = UserStrategyDto.UserTrueName;
                item.MoneyCheckDate = DateTime.Now;
            }
            return await mediator.Send(sheepSaleBUpdateMoneyStateCommand);
        }
        /// <summary>
        /// 批量修改仓库状态
        /// </summary>
        /// <param name="sheepSaleBUpdateMoneyStateCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<bool> BatchUpdateWorkState(SheepSaleBUpdateWorkStateCommand sheepSaleBUpdateWorkStateCommand)
        {
            foreach (var item in sheepSaleBUpdateWorkStateCommand.sheepSaleModels)
            {
                item.UpdateBy = UserStrategyDto.UserTrueName;
                item.UpdateDate=DateTime.Now;
                item.WorkShopCheckPeople = UserStrategyDto.UserTrueName;
                item.WorkShopCheckDate = DateTime.Now;
            }
            return await mediator.Send(sheepSaleBUpdateWorkStateCommand);
        }
    }
}
